﻿using DevExpress.Utils;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraLayout;
using DevExpress.XtraNavBar;
using DevExpress.XtraPivotGrid;
using DevExpress.XtraTreeList;
using DevExpress.XtraVerticalGrid;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace Inaction.UI.Common
{
    public class LayoutHelper
    {
        string filePath = string.Empty;
        string directory = string.Empty;

        Form Form = null;

        IList storeControls = new List<Control>();
        IList layoutControls = null;

        static Dictionary<Form, LayoutHelper> FormLayoutList = new Dictionary<Form, LayoutHelper>();
        public LayoutHelper(Form from, List<Control> layoutControls = null)
        {
            this.Form = from;
            if (Form == null) return;
            from.Shown += new EventHandler(FormShown);
            from.FormClosed += new FormClosedEventHandler(FormClosed);
            directory = Application.StartupPath + @"\Layout\" + from.GetType().Assembly.GetName().Name + @"\" + from.GetType().Name + "_" + Inaction.Security.Principal.Instance.Identity.UserNumber + @"\";
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            if (layoutControls != null)
            {
                this.layoutControls = layoutControls;
            }
            from.Disposed += (sender, e) => { FormLayoutList.Remove(from); };
        }
        private void ResetLayout()
        {
            if (Directory.Exists(directory))
            {
                Directory.Delete(directory, true);
            }
            Form.FormClosed -= FormClosed;
        }

        public static void ResetLayout(Form form)
        {
            if (FormLayoutList.ContainsKey(form) && !form.IsDisposed)
            {
                var layoutHelper = FormLayoutList[form];
                layoutHelper.ResetLayout();
            }
        }
        public static void ResetLayoutAll()
        {
            var directory = Application.StartupPath + @"\Layout\";
            if (Directory.Exists(directory))
            {
                try
                {
                    Directory.Delete(directory, true);
                }
                catch { }
            }
            foreach (var item in FormLayoutList)
            {
                item.Value.ResetLayout();
            }
        }

        void FormShown(object sender, EventArgs e)
        {
            try
            {
                FormLayoutList.Add(Form, this);
                LoadLayout(layoutControls != null ? layoutControls : Form.Controls);
            }
            catch
            {
            }
        }

        void FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                SaveLayout(storeControls);
                FormLayoutList.Remove(Form);
            }
            catch
            {
            }
        }

        #region 加载/保存控件样式
        private void LoadLayout(IList controls)
        {
            foreach (Control control in controls)
            {
                string fileName = directory + control.Name + ".xml";
                if (control is GridControl)
                {
                    var grid = control as GridControl;
                    storeControls.Add(control);
                    if (File.Exists(fileName))
                    {
                        grid.MainView.RestoreLayoutFromXml(fileName, OptionsLayoutGrid.FullLayout);
                    }
                    foreach (RepositoryItem edit in grid.RepositoryItems)
                    {
                        if (edit is RepositoryItemGridLookUpEdit)
                        {
                            var file = directory + edit.Name + ".xml";
                            if (File.Exists(file))
                            {
                                var glue = edit as RepositoryItemGridLookUpEdit;
                                glue.View.RestoreLayoutFromXml(file);
                            }
                        }
                        if (edit is RepositoryItemTreeListLookUpEdit)
                        {
                            var file = directory + edit.Name + ".xml";
                            if (File.Exists(file))
                            {
                                var glue = edit as RepositoryItemTreeListLookUpEdit;
                                glue.TreeList.RestoreLayoutFromXml(file);
                            }
                        }
                    }
                }
                else if (control is LayoutControl)
                {
                    var layout = control as LayoutControl;
                    storeControls.Add(control);
                    if (File.Exists(fileName))
                    {
                        layout.RestoreLayoutFromXml(fileName);
                    }
                }
                else if (control is TreeList)
                {
                    var treeList = control as TreeList;
                    storeControls.Add(control);
                    if (File.Exists(fileName))
                    {
                        treeList.RestoreLayoutFromXml(fileName);
                    }
                    foreach (RepositoryItem edit in treeList.RepositoryItems)
                    {
                        if (edit is RepositoryItemGridLookUpEdit)
                        {
                            var file = directory + edit.Name + ".xml";
                            if (File.Exists(file))
                            {
                                var glue = edit as RepositoryItemGridLookUpEdit;
                                glue.View.RestoreLayoutFromXml(file);
                            }
                        }
                        if (edit is RepositoryItemTreeListLookUpEdit)
                        {
                            var file = directory + edit.Name + ".xml";
                            if (File.Exists(file))
                            {
                                var glue = edit as RepositoryItemTreeListLookUpEdit;
                                glue.TreeList.RestoreLayoutFromXml(file);
                            }
                        }
                    }

                }
                else if (control is PivotGridControl)
                {
                    var grid = control as PivotGridControl;
                    storeControls.Add(control);
                    if (File.Exists(fileName))
                    {
                        grid.RestoreLayoutFromXml(fileName, OptionsLayoutGrid.FullLayout);
                    }
                    foreach (RepositoryItem edit in grid.RepositoryItems)
                    {
                        if (edit is RepositoryItemGridLookUpEdit)
                        {
                            var file = directory + edit.Name + ".xml";
                            if (File.Exists(file))
                            {
                                var glue = edit as RepositoryItemGridLookUpEdit;
                                glue.View.RestoreLayoutFromXml(file);
                            }
                        }
                        if (edit is RepositoryItemTreeListLookUpEdit)
                        {
                            var file = directory + edit.Name + ".xml";
                            if (File.Exists(file))
                            {
                                var glue = edit as RepositoryItemTreeListLookUpEdit;
                                glue.TreeList.RestoreLayoutFromXml(file);
                            }
                        }
                    }
                }
                else if (control is NavBarControl)
                {
                    var navBar = control as NavBarControl;
                    storeControls.Add(control);
                    if (File.Exists(fileName))
                    {
                        navBar.RestoreFromXml(fileName);
                    }
                }
                else if (control is VGridControl)
                {
                    var grid = control as VGridControl;
                    storeControls.Add(control);
                    if (File.Exists(fileName))
                    {
                        grid.RestoreLayoutFromXml(fileName);
                    }
                    foreach (RepositoryItem edit in grid.RepositoryItems)
                    {
                        if (edit is RepositoryItemGridLookUpEdit)
                        {
                            var file = directory + edit.Name + ".xml";
                            if (File.Exists(file))
                            {
                                var glue = edit as RepositoryItemGridLookUpEdit;
                                glue.View.RestoreLayoutFromXml(file);
                            }
                        }
                        if (edit is RepositoryItemTreeListLookUpEdit)
                        {
                            var file = directory + edit.Name + ".xml";
                            if (File.Exists(file))
                            {
                                var glue = edit as RepositoryItemTreeListLookUpEdit;
                                glue.TreeList.RestoreLayoutFromXml(file);
                            }
                        }
                    }

                }
                else if (control is GridLookUpEdit)
                {
                    var glue = control as GridLookUpEdit;
                    storeControls.Add(control);
                    if (File.Exists(fileName))
                    {
                        glue.Properties.View.RestoreLayoutFromXml(fileName);
                    }
                }
                else if (control is TreeListLookUpEdit)
                {
                    var tlue = control as TreeListLookUpEdit;
                    storeControls.Add(control);
                    if (File.Exists(fileName))
                    {
                        tlue.Properties.TreeList.RestoreLayoutFromXml(fileName);
                    }
                }
            }
        }

        private void SaveLayout(IList controls)
        {
            foreach (Control control in controls)
            {
                string fileName = directory + control.Name + ".xml";
                if (control is GridControl)
                {
                    var grid = control as GridControl;
                    grid.MainView.SaveLayoutToXml(fileName, OptionsLayoutGrid.FullLayout);
                    foreach (RepositoryItem edit in grid.RepositoryItems)
                    {
                        if (edit is RepositoryItemGridLookUpEdit)
                        {
                            var file = directory + edit.Name + ".xml";
                            var glue = edit as RepositoryItemGridLookUpEdit;
                            glue.View.SaveLayoutToXml(file);
                        }
                        if (edit is RepositoryItemTreeListLookUpEdit)
                        {
                            var file = directory + edit.Name + ".xml";
                            var glue = edit as RepositoryItemTreeListLookUpEdit;
                            glue.TreeList.SaveLayoutToXml(file);
                        }
                    }
                }
                else if (control is LayoutControl)
                {
                    var layout = control as LayoutControl;
                    layout.SetDefaultLayout();
                    layout.SaveLayoutToXml(fileName);
                }
                else if (control is TreeList)
                {
                    var treeList = control as TreeList;
                    treeList.SaveLayoutToXml(fileName);
                    foreach (RepositoryItem edit in treeList.RepositoryItems)
                    {
                        if (edit is RepositoryItemGridLookUpEdit)
                        {
                            var file = directory + edit.Name + ".xml";
                            var glue = edit as RepositoryItemGridLookUpEdit;
                            glue.View.SaveLayoutToXml(file);
                        }
                        if (edit is RepositoryItemTreeListLookUpEdit)
                        {
                            var file = directory + edit.Name + ".xml";
                            var glue = edit as RepositoryItemTreeListLookUpEdit;
                            glue.TreeList.SaveLayoutToXml(file);
                        }
                    }
                }
                else if (control is PivotGridControl)
                {
                    var pivotGrid = control as PivotGridControl;
                    pivotGrid.SaveLayoutToXml(fileName, OptionsLayoutGrid.FullLayout);
                    foreach (RepositoryItem edit in pivotGrid.RepositoryItems)
                    {
                        if (edit is RepositoryItemGridLookUpEdit)
                        {
                            var file = directory + edit.Name + ".xml";
                            var glue = edit as RepositoryItemGridLookUpEdit;
                            glue.View.SaveLayoutToXml(file);
                        }
                        if (edit is RepositoryItemTreeListLookUpEdit)
                        {
                            var file = directory + edit.Name + ".xml";
                            var glue = edit as RepositoryItemTreeListLookUpEdit;
                            glue.TreeList.SaveLayoutToXml(file);
                        }
                    }
                }
                else if (control is NavBarControl)
                {
                    var navBar = control as NavBarControl;
                    navBar.SaveToXml(fileName);
                }

                else if (control is VGridControl)
                {
                    var grid = control as VGridControl;
                    grid.SaveLayoutToXml(fileName);
                    foreach (RepositoryItem edit in grid.RepositoryItems)
                    {
                        if (edit is RepositoryItemGridLookUpEdit)
                        {
                            var file = directory + edit.Name + ".xml";
                            var glue = edit as RepositoryItemGridLookUpEdit;
                            glue.View.SaveLayoutToXml(file);
                        }
                        if (edit is RepositoryItemTreeListLookUpEdit)
                        {
                            var file = directory + edit.Name + ".xml";
                            var glue = edit as RepositoryItemTreeListLookUpEdit;
                            glue.TreeList.SaveLayoutToXml(file);
                        }
                    }
                }
                else if (control is GridLookUpEdit)
                {
                    var glue = control as GridLookUpEdit;
                    glue.Properties.View.SaveLayoutToXml(fileName);
                }
                else if (control is TreeListLookUpEdit)
                {
                    var tlue = control as TreeListLookUpEdit;
                    tlue.Properties.TreeList.SaveLayoutToXml(fileName);
                }
            }
        }
        #endregion

        public static bool IsLayoutControl(Control control)
        {
            return control is GridControl
                    || (control is LayoutControl
                    || control is TreeList
                    || control is PivotGridControl
                    || control is NavBarControl
                    || control is VGridControl
                    || control is GridLookUpEdit
                    || control is TreeListLookUpEdit);
        }
    }
}
